package com.xiaoxi.bms.service.role.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.user.MenuUserEnums;
import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.domain.role.BMSRoleMenu;
import com.xiaoxi.bms.mapper.role.BMSRoleMenuMapper;
import com.xiaoxi.bms.service.menu.BMSMenuService;
import com.xiaoxi.bms.service.role.BMSRoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName BMSRoleMenuServiceImpl
 * @Description
 * @Author xiaoxi
 * @Date 2022/7/3 19:05
 * @Version 1.0
 */
@Service
@Slf4j
public class BMSRoleMenuServiceImpl extends ServiceImpl<BMSRoleMenuMapper, BMSRoleMenu> implements BMSRoleMenuService {

    @Resource
    private BMSRoleMenuMapper bmsRoleMenuMapper;

    @Resource
    private BMSMenuService bmsMenuService;

    @Override
    public List<Long> getRoleMenuListByRoleId(Long roleId) {
        QueryWrapper<BMSRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<Long> menuIds = new ArrayList<>();
        bmsRoleMenuMapper.selectList(queryWrapper).forEach(bmsRoleMenu -> {
            menuIds.add(bmsRoleMenu.getMenuId());
        });
        return menuIds;
    }

    @Override
    public void deleteRoleMenuByRoleId(Long roleId) {
        UpdateWrapper<BMSRoleMenu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id", roleId);
        bmsRoleMenuMapper.delete(updateWrapper);
    }

    @Override
    public void deleteRoleMenuByMenuIds(List<Long> menuIdList) {

        UpdateWrapper<BMSRoleMenu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("menu_id", menuIdList);
        bmsRoleMenuMapper.delete(updateWrapper);

    }

    @Override
    public void addRoleMenuByRoleId(Long roleId, List<Long> roleMenuIdList) throws BMSException {

        if (Objects.isNull(roleMenuIdList) || roleMenuIdList.size() == 0) {
            log.warn("{} : 未传对应的菜单编号", roleId);
            return;
        }

        boolean check = bmsMenuService.checkMenuIdListValid(roleMenuIdList);
        if (!check) {
            throw new BMSException(MenuUserEnums.MENU_ID_NOT_EXIST);
        }

        List<BMSRoleMenu> bmsRoleMenuList = new ArrayList<>(roleMenuIdList.size());
        roleMenuIdList.forEach(menuId -> {
            BMSRoleMenu bmsRoleMenu = new BMSRoleMenu();
            bmsRoleMenu.setRoleId(roleId);
            bmsRoleMenu.setMenuId(menuId);
            bmsRoleMenuList.add(bmsRoleMenu);
        });
        saveBatch(bmsRoleMenuList);
    }

    @Override
    public void updateRoleMenu(Long roleId, List<Long> roleMenuIdList) throws BMSException {
        deleteRoleMenuByRoleId(roleId);
        addRoleMenuByRoleId(roleId, roleMenuIdList);
    }
}
